<template>
  <el-card>
    <div class="flex justify-between items-center mb-20px">
      <h4>查看详情</h4>
      <el-button
        @click="
          () => {
            $router.back()
            delVisitedView(route)
          }
        "
        v-if="detail?.isFreeMeeting == 0"
        >返回</el-button
      >
    </div>
    <!-- <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="会议id" prop="meetingId">
        <el-input v-model="formData.meetingId" placeholder="请输入会议id" />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户id" />
      </el-form-item>
      <el-form-item label="状态（0待审核 1已通过 2已驳回）" prop="isFree">
        <el-input v-model="formData.isFree" placeholder="请输入状态（0待审核 1已通过 2已驳回）" />
      </el-form-item>
    </el-form> -->
    <el-row class="mb-20px">
      <el-col :span="2"> 报名ID： </el-col>
      <el-col :span="8">{{ formData.id }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 会议状态： </el-col>
      <el-col :span="8"> {{ formData.meetingStatus }}</el-col>
    </el-row>
    <!-- 审核 -->
    <div v-if="detail?.isFreeMeeting == 1">
      <el-row class="mb-20px">
        <el-col :span="2"> 审核状态： </el-col>
        <el-col :span="8">
          <el-radio-group v-model="formData.isFree" :disabled="detail.isFree != 0">
            <el-radio :value="0">待审核</el-radio>
            <el-radio :value="1">已通过</el-radio>
            <el-radio :value="2">已驳回</el-radio>
          </el-radio-group>
        </el-col>
      </el-row>
      <el-row class="mb-20px" v-if="formData.isFree == 2">
        <el-col :span="2"> 驳回原因： </el-col>
        <el-col :span="8">
          <el-input
            v-model="formData.rejectionReason"
            type="textarea"
            placeholder="请输入驳回原因"
            :rows="6"
            resize="none"
            :disabled="detail.isFree != 0"
          />
        </el-col>
      </el-row>
    </div>
    <el-row class="mb-20px">
      <el-col :span="2"> 用户ID： </el-col>
      <el-col :span="8">{{ formData.userId }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 用户昵称： </el-col>
      <el-col :span="8">{{ formData.nickname }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 用户手机号： </el-col>
      <el-col :span="8">{{ formData.mobile }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 参会方案： </el-col>
      <el-col :span="8">{{ formData.schemeName }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 支付金额： </el-col>
      <el-col :span="8">{{ formData.schemePrice || '-' }}元 </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 参会人姓名： </el-col>
      <el-col :span="8">{{ formData.attendeeName }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 所属分类： </el-col>
      <el-col :span="8">{{ formData.category }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 手机号码： </el-col>
      <el-col :span="8">{{ formData.phone }} </el-col>
    </el-row>
    <!-- <el-row class="mb-20px">
      <el-col :span="2"> 公司： </el-col>
      <el-col :span="8">{{ formData.company }} </el-col>
    </el-row> -->
    <el-row class="mb-20px">
      <el-col :span="2"> 部门： </el-col>
      <el-col :span="8">{{ formData.department }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 职位： </el-col>
      <el-col :span="8">{{ formData.position }} </el-col>
    </el-row>
    <el-row class="mb-20px" v-for="(value, key) in registrationNames" :key="key">
      <el-col :span="2"> {{ key }}： </el-col>
      <el-col :span="8">{{ value }} </el-col>
    </el-row>
    <div v-if="detail?.isFreeMeeting == 1">
      <h4 class="mb-20px">发票信息</h4>
      <el-row class="mb-20px">
        <el-col :span="2"> 发票类型： </el-col>
        <el-col :span="8">{{ formData.invoiceType || '-' }} </el-col>
      </el-row>
      <el-row class="mb-20px">
        <el-col :span="2"> 单位名称： </el-col>
        <el-col :span="8">{{ formData.invoiceUnitName || '-' }} </el-col>
      </el-row>
      <el-row class="mb-20px">
        <el-col :span="2"> 电话： </el-col>
        <el-col :span="8">{{ formData.invoicePhone || '-' }} </el-col>
      </el-row>
      <el-row class="mb-20px">
        <el-col :span="2"> 税号： </el-col>
        <el-col :span="8">{{ formData.invoiceTaxId || '-' }} </el-col>
      </el-row>
      <el-row class="mb-20px">
        <el-col :span="2"> 开户行： </el-col>
        <el-col :span="8">{{ formData.invoiceOpeningBank || '-' }} </el-col>
      </el-row>
      <el-row class="mb-20px">
        <el-col :span="2"> 账号： </el-col>
        <el-col :span="8">{{ formData.invoiceAccountNumber || '-' }} </el-col>
      </el-row>
      <el-row class="mb-20px">
        <el-col :span="2"> 发票地址： </el-col>
        <el-col :span="8">{{ formData.invoiceAddress || '-' }} </el-col>
      </el-row>
    </div>
    <h4 class="mb-20px">会议信息</h4>
    <el-row class="mb-20px">
      <el-col :span="2"> 是否为免费会议： </el-col>
      <el-col :span="8">{{ formData.isFreeMeeting ? '否' : '是' }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 会议名称： </el-col>
      <el-col :span="8">{{ formData.meetingName }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 会议时间： </el-col>
      <el-col :span="8"
        >{{ formatDate(formData.startTime) }} - {{ formatDate(formData.endTime) }}
      </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 会议地址： </el-col>
      <el-col :span="8">{{ formData.meetingAddress }} </el-col>
    </el-row>
    <el-row class="mb-20px">
      <el-col :span="2"> 创建时间： </el-col>
      <el-col :span="8">{{ formatDate(formData.createTime) }} </el-col>
    </el-row>
    <div>
      <el-button
        v-if="detail?.isFreeMeeting == 1 && detail?.isFree == 0"
        @click="review"
        type="primary"
        :disabled="formLoading"
        >确 定</el-button
      >
      <el-button
        v-if="detail?.isFreeMeeting == 1"
        @click="
          () => {
            $router.back()
            delVisitedView(route)
          }
        "
        >取 消</el-button
      >
    </div>
  </el-card>
</template>
<script setup lang="ts">
import {
  ConferenceRegistrationInformationApi,
  ConferenceRegistrationInformation
} from '@/api/biz/conferenceregistrationinformation'
import { formatDate } from '@/utils/formatTime'
import { useTagsViewStore } from '@/store/modules/tagsView'

const { delVisitedView } = useTagsViewStore()
const route = useRoute()
const router = useRouter()

/** 会议报名信息 表单 */
defineOptions({ name: 'ConferenceRegistrationInformationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const id = +useRoute().params.id

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  meetingId: undefined,
  userId: undefined,
  isFree: 0,
  meetingStatus: undefined,
  nickname: undefined,
  mobile: undefined,
  schemeName: undefined,
  schemePrice: undefined,
  attendeeName: undefined,
  category: undefined,
  phone: undefined,
  company: undefined,
  department: undefined,
  position: undefined,
  isFreeMeeting: undefined,
  meetingName: undefined,
  startTime: new Date(),
  endTime: new Date(),
  meetingAddress: undefined,
  createTime: new Date(),
  rejectionReason: undefined,
  invoiceType: undefined,
  invoiceUnitName: undefined,
  invoicePhone: undefined,
  invoiceTaxId: undefined,
  invoiceOpeningBank: undefined,
  invoiceAccountNumber: undefined,
  invoiceAddress: undefined
})
const detail = ref()
const registrationNames = computed(() => {
  if (detail.value?.registrationNames) {
    return JSON.parse(detail.value?.registrationNames)
  }
  return {}
})
const formRules = reactive({
  meetingId: [{ required: true, message: '会议id不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '用户id不能为空', trigger: 'blur' }],
  isFree: [{ required: true, message: '状态（0待审核 1已通过 2已驳回）不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      const data =
        await ConferenceRegistrationInformationApi.getConferenceRegistrationInformation(id)
      formData.value = data
      detail.value = { ...data }
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ConferenceRegistrationInformation
    if (formType.value === 'create') {
      await ConferenceRegistrationInformationApi.createConferenceRegistrationInformation(data)
      message.success(t('common.createSuccess'))
    } else {
      await ConferenceRegistrationInformationApi.updateConferenceRegistrationInformation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const review = () => {
  ConferenceRegistrationInformationApi.audit({
    id: formData.value.id,
    isFree: formData.value.isFree,
    rejectionReason: formData.value.rejectionReason
  }).then(() => {
    message.success('审核成功')
    router.back()
    delVisitedView(route)
  })
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    meetingId: undefined,
    userId: undefined,
    isFree: 0,
    meetingStatus: undefined,
    nickname: undefined,
    mobile: undefined,
    schemeName: undefined,
    schemePrice: undefined,
    attendeeName: undefined,
    category: undefined,
    phone: undefined,
    company: undefined,
    department: undefined,
    position: undefined,
    isFreeMeeting: undefined,
    meetingName: undefined,
    startTime: new Date(),
    endTime: new Date(),
    meetingAddress: undefined,
    createTime: new Date(),
    rejectionReason: undefined,
    invoiceType: undefined,
    invoiceUnitName: undefined,
    invoicePhone: undefined,
    invoiceTaxId: undefined,
    invoiceOpeningBank: undefined,
    invoiceAccountNumber: undefined,
    invoiceAddress: undefined
  }
  formRef.value?.resetFields()
}

open('update', id)
</script>
